import { MovieClip } from "../mc/MovieClip";

export module fairygui {
    export class GButton extends fgui.GButton {

    }
    export class UIPackage extends fgui.UIPackage {

    }
    export class UIObjectFactory extends fgui.UIObjectFactory {

    }
    export class GComboBox extends fgui.GComboBox {

    }
    export class GComponent extends fgui.GComponent {
        public dispose() {
            Laya.timer.clearAll(this);
            super.dispose();
        }

        public close() {
            Laya.timer.clearAll(this);
        }

        // 更新点击测试区域  确保图片已加载 默认 x y 为 0
        public updateHitTestArea(url: string) {
            let packageItem: any = fgui.UIPackage.getItemByURL(url);
            if (packageItem != null && packageItem.pixelHitTestData != null) {
                this._displayObject.hitArea = new fgui.PixelHitTest(packageItem.pixelHitTestData, 0, 0);
                this._displayObject.mouseThrough = false;
                this._displayObject.hitTestPrior = true;
            }
        }

        public getChildByName(name: string) {
            let cnt: number = this._children.length;
            for (let i: number = 0; i < cnt; ++i) {
                if (this._children[i].name == name)
                    return this._children[i];
            }
            return null;
        }
    }

    export class GGraph extends fgui.GGraph {

    }

    export class GGroup extends fgui.GGroup {

    }

    export class GImage extends fgui.GImage {

    }
    export class GLabel extends fgui.GLabel {
        public set icon(value: string) {
            this.icon = value;
        }
    }

    export class GList extends fgui.GList {
    }

    export class Controller extends fgui.Controller {
    }

    export class Transition extends fgui.Transition {
    }

    export class GLoader extends fgui.GLoader {
        /**
         * 赋值并清理上一次的资源   ''可作为清理
         */
        public set urlAndClearLast(value: string) {
            if (this.url && this.url != value) {
                if (!fgui.ToolSet.startsWith(this.url, "ui://"))
                    Laya.loader.clearTextureRes(this.url);
                let movie = this.displayObject.destroyed ? null : this.content;
                if (movie && movie instanceof fgui.MovieClip) {
                    let packItem = this['_contentItem'] as fgui.PackageItem;
                    MovieClip.clearMcTimesByType(packItem ? packItem.owner.name : '');
                }
            }
            this.url = value;
        }

        //loader 动画   暂不支持组件
        public setUrlByPack(pkgName: string, itemName: string) {
            let pkgItem: fgui.UIPackage = fgui.UIPackage.getByName(pkgName);
            if (pkgItem && Laya.loader.getRes(pkgItem.customId + '.fui')) {
                this.urlAndClearLast = fgui.UIPackage.getItemURL(pkgName, itemName);
                if (this.content && this.content instanceof fgui.MovieClip) {
                    MovieClip.addMcNode(pkgName);
                }
            }
            else {
                this.urlAndClearLast = '';
                MovieClip.loadMcRes(pkgName, Laya.Handler.create(this, () => {
                    if (!this._displayObject.destroyed && this.url == '') {
                        this.setUrlByPack(pkgName, itemName);
                    }
                }));
            }
        }

        public set sizeGrid(value: string) {
            if (this.content instanceof Laya.Image) {
                this.content.sizeGrid = value;
            }
        }

        dispose() {
            this.urlAndClearLast = '';
            super.dispose();
        }
    }

    export class GMovieClip extends fgui.GMovieClip {
        public stop() {
            this.playing = false;
        }

        public stopAtFrame(value: number) {
            this.stop();
            this.frame = value;
        }

        dispose() {
            this.stop(); //销毁的动画无完成回调
            super.dispose();
        }

        public playOnce(endHandler: Laya.Handler, dispose: boolean = true, endFrame: number = -1) {
            endFrame = dispose ? -1 : endFrame;
            this.setPlaySettings(0, -1, 1, endFrame, Laya.Handler.create(this, (endHandler: Laya.Handler) => {
                endHandler && endHandler.run();
                // utils.UseUtils.handlerRun(endHandler);
                dispose && this.dispose();
            }, [endHandler]));
        }
    }

    export class GProgressBar extends fgui.GProgressBar {
    }

    export class GRichText extends fgui.GRichTextField {
    }

    export class GScrollBar extends fgui.GScrollBar {

    }

    export class GSlider extends fgui.GSlider {

    }

    export class GTextField extends fgui.GBasicTextField {

    }

    export class GTextInput extends fgui.GTextInput {

    }
}